
package io.dbw.modules.business.controller.sys;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.dbw.common.annotation.SysLog;
import io.dbw.common.enums.BusinessTypeEnum;
import io.dbw.common.utils.*;
import io.dbw.modules.business.entity.SysConfigEntity;
import io.dbw.modules.business.service.SysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * 系统配置信息
 *
 * 
 */
@RestController
@RequestMapping("/sys/config")
@Api(tags = "系统配置管理")
public class SysConfigController extends AbstractController {
	@Autowired
	private SysConfigService sysConfigService;
	
	/**
	 * 所有配置列表
	 */
	@GetMapping("/list")
	@ApiOperation(value="列表查询")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query",name = "pageNo",value ="第几页",dataType ="int",required = true),
			@ApiImplicitParam(paramType = "query",name = "pageSize",value ="每页条数",dataType ="int",required = true),
			@ApiImplicitParam(paramType = "query",name = "paramKey",value ="键",dataType ="string" ),
	})
	public Result<PageUtils<SysConfigEntity>> list(@RequestParam Map<String, Object> params){
		PageUtils<SysConfigEntity> page = sysConfigService.queryPage(params);
		return writeSuccess(page);
	}
	
	/**
	 * 所有配置列表
	 */
	@GetMapping("/getConfigListByParamType")
	@ApiOperation(value="根据类型查询配置项")
	@ApiImplicitParam(paramType = "query",name = "paramType",value ="类型;1:查找洲名称",dataType ="int",required = true)
	public Result<List<SysConfigEntity>> getConfigListByParamType(@NotNull(message = "类型不能为空") Integer paramType){
		List<SysConfigEntity> configList = sysConfigService.getConfigListByParamType(paramType);
		return writeSuccess(configList);
	}

	/**
	 * 查询配置信息
	 */
	@GetMapping("/getConfigInfoByParamKey")
	@ApiOperation(value="根据KEY查询配置信息")
	@ApiImplicitParam(paramType = "query",name = "paramKey",value ="键",dataType ="String",required = true)
	public Result<SysConfigEntity> getConfigInfoByParamKey(@NotBlank(message = "键值不能为空") String paramKey){
		SysConfigEntity configEntity = sysConfigService.getOne(new QueryWrapper<SysConfigEntity>().eq("param_key",paramKey));
		return writeSuccess(configEntity);
	}

	/**
	 * 获取阿里云图片路径前缀
	 */
	@GetMapping("/getAccessPath")
	@ApiOperation(value="获取阿里云图片路径前缀")
	public Result<String> getAccessPath(){
		String accessPath="";
		String sVal=sysConfigService.getValue(Constant.ALIBABA_PARAM);
		Map<String,Object> map = JsonUtil.jsonToObject(sVal, Map.class);
		if (!CollectionUtils.isEmpty(map)){
			accessPath= MapUtils.getString(map,"accessPath");
		}
		return writeSuccess(accessPath);
	}
	
	/**
	 * 保存配置
	 */
	@SysLog(value = "配置",businessType = BusinessTypeEnum.INSERT)
	@PostMapping("/save")
	@ApiOperation(value="保存")
	public Result save(@RequestBody SysConfigEntity config){
		sysConfigService.saveConfig(config);
		return writeSuccess();
	}
	
	/**
	 * 修改配置
	 */
	@SysLog(value = "配置",businessType = BusinessTypeEnum.UPDATE)
	@PostMapping("/update")
	@ApiOperation(value="修改")
	public Result update(@RequestBody SysConfigEntity config){
		AssertUtils.isNull(config.getId(),"ID不能为空");
		sysConfigService.update(config);
		return writeSuccess();
	}
	
	/**
	 * 删除配置
	 */
	@SysLog(value = "配置",businessType = BusinessTypeEnum.DELETE)
	@PostMapping("/delete")
	@ApiOperation(value="删除")
	@ApiImplicitParam(paramType = "body",name = "list",value ="id集合",dataType ="List",required = true)
	public Result delete(@RequestBody List<Long> list){
		sysConfigService.deleteBatch(list);
		return writeSuccess();
	}

}
